package com.link.base.base.menupageurls.utils;

import com.link.base.base.common.model.Menu;
import com.link.base.base.common.service.MenuService;
import com.link.base.base.menupageurls.service.MenuPageUrlsService;
import com.link.base.enterprise.model.CoreEnterprise;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.interceptor.SecurityInterceptor;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.Filter;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.BeanTools;
import com.link.core.util.StringUtils;

import java.util.List;

/***
 * 增加页面url的线程类
 *
 * @author LiYang
 * @date 2019-12-16
 **/
public class RunAddPageUrl implements Runnable {

    private String token;
    private CoreUser user;
    private CoreEnterprise enterprise;


    public RunAddPageUrl(String token, CoreUser user, CoreEnterprise enterprise) {
        this.token = token;
        this.user = user;
        this.enterprise = enterprise;
    }

    @Override
    public void run() {
        try {
            SecurityInterceptor.token.set(token);
            SecurityInterceptor.tempUser.set(user);
            SecurityInterceptor.tempEnterpirse.set(enterprise);
            // 查询所有的一级菜单
            QueryParams qpsTemp = new QueryParams();
            qpsTemp.addFilter(new Filter("menuGrade", "0"));
            qpsTemp.setPageFlag(false);
            Menu t = (Menu) BasicModel.transformClass(new Menu(), qpsTemp);
            MenuService menuService = (MenuService) BeanTools.getBean("menuServiceImpl");
            MenuPageUrlsService menuPageUrlsService = (MenuPageUrlsService) BeanTools.getBean("menuPageUrlsServiceImpl");
            List<Menu> list = menuService.queryByExamplePage(t);
            for (Menu menu : list) {
                // 查询全部菜单，菜单不会很多，不会出现内存溢出
                QueryParams qps = new QueryParams();
                qps.setPageFlag(false);
                qps.addFilter(new Filter("parentMenuId", menu.getId().toString()));
                menu = Menu.transformClass(new Menu(), qps);
                List<Menu> menus = menuService.queryByExamplePage(menu);
                for (Menu menu1 : menus) {
                    if (menu1.getMenuUrl() != null && StringUtils.isNotBlank(menu1.getPlatformType())) {
                        menuPageUrlsService.addPageUrlAll(menu1);
                    }
                }
            }
        } catch (Exception e) {
            throw new BasicServiceException("增加页面url的线程类错误" + e.getMessage());
        }
    }
}
